﻿using System.Drawing.Drawing2D;

namespace 设备报警系统客户端;

public class VerificationServices
{
    public static void Start(out Bitmap captchaImage, out string validationKey)
    {
        GenerateCaptchaImage(GenerateRandomCaptchaText(), out captchaImage, out validationKey);
    }

    public static System.Drawing.Image CropToCircle(Color color, int diameter)
    {
        System.Drawing.Image img = GetPicture(color);
        // 确保直径不大于图片的宽度或高度  
        int width = img.Width;
        int height = img.Height;
        int minSize = Math.Min(width, height);
        diameter = Math.Min(diameter, minSize);

        // 创建一个新的Bitmap，大小为直径x直径  
        Bitmap bitmap = new Bitmap(diameter, diameter);

        // 创建一个Graphics对象，用于绘制  
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            // 设置高质量插值和平滑模式  
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;

            // 计算圆的矩形边界（圆心在Bitmap的中心）  
            Rectangle rect = new Rectangle(0, 0, diameter, diameter);

            // 创建一个新的路径并添加一个圆  
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddEllipse(rect);

                // 设置裁剪区域为圆形  
                g.SetClip(path);

                // 绘制源图像到新的Bitmap中，同时根据需要进行缩放  
                float scale = (float)diameter / (float)Math.Max(img.Width, img.Height);
                int newWidth = (int)(img.Width * scale);
                int newHeight = (int)(img.Height * scale);
                int x = (diameter - newWidth) / 2;
                int y = (diameter - newHeight) / 2;
                g.DrawImage(img, new Rectangle(x, y, newWidth, newHeight));
            }

            // 重置裁剪区域  
            g.ResetClip();
        }

        // 返回裁剪后的圆形图片  
        return bitmap;
    }

    public static Bitmap GetPicture(Color color)
    {
        // 图片的宽度和高度（假设它们是相同的，以生成一个完美的圆形）  
        int width = 100;
        // 创建一个新的Bitmap对象，大小为width x height  

        Bitmap bitmap = new Bitmap(width, width);
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.Clear(color);
        }
        return bitmap;
    }

    private static void GenerateCaptchaImage(string captchaText, out Bitmap captchaImage, out string validationKey)
    {
        // 设置验证码图片的尺寸  
        int width = 200;
        int height = 60;

        // 创建验证码图片  
        captchaImage = new Bitmap(width, height);

        // 创建绘图对象  
        using (Graphics g = Graphics.FromImage(captchaImage))
        {
            // 设置绘图质量  
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.Clear(Color.White);

            // 设置字体和颜色  
            System.Drawing.Font font = new System.Drawing.Font("Arial", 20, FontStyle.Bold);
            Brush textBrush = new SolidBrush(Color.FromArgb(30, 50, 100));

            // 绘制验证码文本  
            Random random = new Random();
            float x = random.Next(0, width - 110);
            float y = random.Next(0, height - 25);
            g.DrawString(captchaText, font, textBrush, x, y);

            // 添加噪点  
            for (int i = 0; i < 100; i++)
            {
                int xPos = random.Next(width);
                int yPos = random.Next(height);
                g.FillEllipse(Brushes.Black, xPos, yPos, 1, 1);
            }

            // 添加干扰线  
            for (int i = 0; i < 5; i++)
            {
                int x1 = random.Next(width);
                int y1 = random.Next(height);
                int x2 = random.Next(width);
                int y2 = random.Next(height);
                g.DrawLine(Pens.Gray, x1, y1, x2, y2);
            }
        }
        // 生成验证码的验证键  
        validationKey = captchaText;
    }

    private static string GenerateRandomCaptchaText()
    {
        // 创建一个Random实例  
        Random random = new Random();

        // 生成一个随机的6位数字  
        int randomSixDigitNumber = random.Next(100000, 1000000); // 包含100000和999999 

        return randomSixDigitNumber.ToString();
    }
}

